package com.koushikdutta.ion.cookie;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;
import android.util.Log;
import com.koushikdutta.async.http.AsyncHttpClientMiddleware.OnHeadersReceivedData;
import com.koushikdutta.async.http.AsyncHttpClientMiddleware.OnSocketData;
import com.koushikdutta.async.http.SimpleMiddleware;
import com.koushikdutta.async.http.libcore.RawHeaders;
import java.net.CookieManager;
import java.net.HttpCookie;
import java.net.URI;
import java.util.List;

public class CookieMiddleware extends SimpleMiddleware {
    CookieManager manager;
    SharedPreferences preferences;

    public CookieMiddleware(Context context, String name) {
        reinit(context, name);
    }

    public void reinit(Context context, String name) {
        this.manager = new CookieManager(null, null);
        this.preferences = context.getSharedPreferences(name + "-cookies", 0);
        for (String key : this.preferences.getAll().keySet()) {
            try {
                String value = this.preferences.getString(key, null);
                RawHeaders headers = new RawHeaders();
                boolean first = true;
                for (String line : value.split("\n")) {
                    if (first) {
                        first = false;
                        headers.setStatusLine(line);
                    } else if (!TextUtils.isEmpty(line)) {
                        headers.addLine(line);
                    }
                }
                this.manager.put(URI.create(key), headers.toMultimap());
            } catch (Exception e) {
                Log.e("Ion", "unable to load cookies", e);
            }
        }
    }

    public void onSocket(OnSocketData data) {
        try {
            data.request.getHeaders().addCookies(this.manager.get(URI.create(data.request.getUri().toString()), data.request.getHeaders().getHeaders().toMultimap()));
        } catch (Exception e) {
        }
    }

    public void onHeadersReceived(OnHeadersReceivedData data) {
        try {
            put(URI.create(data.request.getUri().toString()), data.headers.getHeaders());
        } catch (Exception e) {
        }
    }

    public void put(URI uri, RawHeaders headers) {
        try {
            this.manager.put(uri, headers.toMultimap());
            if (headers.get("Set-Cookie") != null) {
                List<HttpCookie> cookies = this.manager.getCookieStore().get(uri);
                RawHeaders dump = new RawHeaders();
                dump.setStatusLine("HTTP/1.1 200 OK");
                for (HttpCookie cookie : cookies) {
                    dump.add("Set-Cookie", cookie.getName() + "=" + cookie.getValue());
                }
                this.preferences.edit().putString(uri.getScheme() + "://" + uri.getAuthority(), dump.toHeaderString()).commit();
            }
        } catch (Exception e) {
        }
    }
}
